package jnpf.controller;



import cn.hutool.core.util.StrUtil;

import io.swagger.annotations.ApiOperation;
import jnpf.common.enums.dify.DifyAiEnum;
import jnpf.model.common.TalkRequest;
import jnpf.model.dify.talk.req.ReNameReq;
import jnpf.model.dify.talk.resp.DifyTalkListConvertResp;
import jnpf.model.dify.talk.resp.TalkHistoryResp;
import jnpf.service.DifyTalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/dify/talk")
public class DifyTalkController {

    @Autowired
    private DifyTalkService difyTalkService;


    @PostMapping("/stream")
    @ApiOperation(value = "AI流式对话", notes = "对话AI")
    public Flux<String> streamCreateAi(@RequestBody TalkRequest talkRequest) {
        return difyTalkService.streamTalkAI(talkRequest);
    }


    @GetMapping("/conversations/{aiType}")
    @ApiOperation(value = "获取会话列表", notes = "用于获取所有的会话列表信息，依靠服务层逻辑进行查询并返回结果")
    public ResponseEntity<List<DifyTalkListConvertResp>> getConversationList(@PathVariable Integer aiType) {
        if (!DifyAiEnum.getTypeList().contains(aiType)) {
            return new ResponseEntity<>(new ArrayList<>(), HttpStatus.BAD_REQUEST);
        }
        try {
            List<DifyTalkListConvertResp> result = difyTalkService.getConversationList(aiType);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(new ArrayList<>(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/messages/{conversationId}/{aiType}")
    @ApiOperation(value = "获取会话历史消息", notes = "用于获取所有的会话历史消息，通过服务层逻辑查询并返回相应数据")
    public ResponseEntity<List<TalkHistoryResp>> getChatHistoryMessages(@PathVariable("conversationId") String conversationId, @PathVariable("aiType") Integer aiType) {
        if(StrUtil.isEmpty(conversationId)){
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        if(aiType==null){
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        try {
            List<TalkHistoryResp> chatHistoryMessages = difyTalkService.getChatHistoryMessages(conversationId, aiType);
            return new ResponseEntity<>(chatHistoryMessages, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/rename/{aiType}")
    @ApiOperation(value = "会话重命名", notes = "依据传入的会话ID（conversation_id）对相应的会话进行重命名操作，通过服务层逻辑实现重命名功能")
    public ResponseEntity<String> renameConversation(@RequestBody @Valid ReNameReq reNameReq, @PathVariable("aiType") Integer aiType) {
        if(aiType==null){
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        try {
            String result = difyTalkService.renameConversation(reNameReq,aiType);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("会话重命名失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @GetMapping("/delete/{conversation_id}/{aiType}")
    @ApiOperation(value = "删除会话", notes = "根据传入的会话ID（conversation_id）删除对应的会话，调用服务层逻辑执行删除操作")
    public ResponseEntity<String> deleteConversation(@PathVariable("conversation_id") String conversationId,@PathVariable("aiType") Integer aiType) {
        try {
            String result = difyTalkService.deleteConversation(conversationId,aiType);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("删除会话失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


//
//    @PostMapping("/{task_id}/stop")
//    @ApiOperation(value = "停止对话", notes = "根据传入的任务ID（task_id）来终止对应的对话，调用服务层逻辑执行终止操作")
//    public ResponseEntity<String> stopChat(@PathVariable("task_id") String taskId) {
//        try {
//            String result = difyTalkService.stopChat(taskId);
//            return new ResponseEntity<>(result, HttpStatus.OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResponseEntity<>("终止对话失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }

//    //开始必填字段数据获取，
//    @PostMapping("/parameters")
//    @ApiOperation(value = "获取应用配置信息", notes = "用于获取应用的配置相关信息，调用服务层逻辑查询并返回对应数据")
//    public ResponseEntity<String> getAppParameters(@RequestBody UserReq userReq) {
//        try {
//            String result = difyTalkService.getAppParameters(userReq);
//            return new ResponseEntity<>(result, HttpStatus.OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResponseEntity<>("获取应用配置信息失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }


//    @GetMapping("/messages/{message_id}/suggested")
//    @ApiOperation(value = "获取下一轮建议问题列表", notes = "借助传入的消息ID（message_id）获取与之对应的下一轮建议问题列表，调用服务层逻辑获取数据")
//    public ResponseEntity<String> getSuggestedQuestions(@PathVariable("message_id") String messageId, @RequestBody Map<String, Object> requestParams) {
//        try {
//            String result = difyTalkService.getSuggestedQuestions(messageId, requestParams);
//            return new ResponseEntity<>(result, HttpStatus.OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResponseEntity<>("获取下一轮建议问题列表失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }

//    @PostMapping("/files/upload")
//    @ApiOperation(value = "上传文件", notes = "接收要上传的文件相关参数，调用服务层逻辑来实现文件上传功能")
//    public ResponseEntity<String> uploadFile(@RequestBody Map<String, Object> requestParams) {
//        try {
//            String result = difyTalkService.uploadFile(requestParams);
//            return new ResponseEntity<>(result, HttpStatus.OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResponseEntity<>("上传文件失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }


//    @PostMapping("/messages/{message_id}/feedbacks")
//    @ApiOperation(value = "消息点赞", notes = "依据传入的消息ID（message_id）对相应的消息进行点赞操作，通过服务层逻辑实现点赞功能")
//    public ResponseEntity<String> likeMessage(@PathVariable("message_id") String messageId, @RequestBody Map<String, Object> requestParams) {
//        try {
//            String result = difyTalkService.likeMessage(messageId, requestParams);
//            return new ResponseEntity<>(result, HttpStatus.OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResponseEntity<>("消息点赞失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }

//    @PostMapping("/audio-to-text")
//    @ApiOperation(value = "语音转文字", notes = "接收语音相关数据，调用服务层逻辑将语音转换为文字内容")
//    public ResponseEntity<String> audioToText(@RequestBody Map<String, Object> requestParams) {
//        try {
//            String result = difyTalkService.audioToText(requestParams);
//            return new ResponseEntity<>(result, HttpStatus.OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResponseEntity<>("语音转文字失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }

//    @PostMapping("/text-to-audio")
//    @ApiOperation(value = "文字转语音", notes = "接收文字相关数据，通过服务层逻辑将文字转换为语音内容")
//    public ResponseEntity<String> textToAudio(@RequestBody Map<String, Object> requestParams) {
//        try {
//            String result = difyTalkService.textToAudio(requestParams);
//            return new ResponseEntity<>(result, HttpStatus.OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResponseEntity<>("文字转语音失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }

//    @PostMapping("/meta")
//    @ApiOperation(value = "获取应用Meta信息", notes = "用于获取应用的Meta相关信息，依靠服务层逻辑查询并返回相应数据")
//    public ResponseEntity<String> getAppMeta(@RequestBody Map<String, Object> requestParams) {
//        try {
//            String result = difyTalkService.getAppMeta(requestParams);
//            return new ResponseEntity<>(result, HttpStatus.OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResponseEntity<>("获取应用Meta信息失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }
}
